/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InterfazAG.javam	
 *
 * Created on 18-mar-2011, 17:54:24
 */

package AG;

import Herramientas.DatosGraficas;
import Herramientas.Graficas;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JSlider;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;

/**
 *
 * @author Francisco
 */
public class InterfazAG extends javax.swing.JFrame {

    /** Creates new form InterfazAG */
    DatosGraficas dt = null;
    public InterfazAG() {
        initComponents();
        
        

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        PanelDatos = new javax.swing.JPanel();
        PanelGraficas = new javax.swing.JScrollPane();
        PanelResultado = new javax.swing.JTextPane();
        CbElitismo = new javax.swing.JCheckBox();
        jSlider2 = new javax.swing.JSlider();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jSlider1 = new javax.swing.JSlider();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        Ruleta = new javax.swing.JRadioButton();
        Torneo = new javax.swing.JRadioButton();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        Estocastico = new javax.swing.JRadioButton();
        Restos = new javax.swing.JRadioButton();
        Ranking = new javax.swing.JRadioButton();
        tipoCruceJ = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        tipoMutacionJ = new javax.swing.JComboBox();
        jLabel10 = new javax.swing.JLabel();
        presionSelectiva = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        SeleccionPropia = new javax.swing.JRadioButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Parte Obligatoria");

        PanelDatos.setLayout(new java.awt.GridLayout(1, 0));

        PanelResultado.setEditable(false);
        PanelGraficas.setViewportView(PanelResultado);

        CbElitismo.setFont(new java.awt.Font("Comic Sans MS", 1, 14));
        CbElitismo.setText("Elitismo");

        jSlider2.setMajorTickSpacing(5);
        jSlider2.setMaximum(30);
        jSlider2.setMinorTickSpacing(2);
        jSlider2.setPaintLabels(true);
        jSlider2.setPaintTicks(true);
        jSlider2.setValue(10);

        jTextField1.setText("100");

        jLabel2.setFont(new java.awt.Font("Comic Sans MS", 1, 14));
        jLabel2.setText("Poblacion");

        jTextField2.setText("100");

        jLabel3.setFont(new java.awt.Font("Comic Sans MS", 1, 14));
        jLabel3.setText("NumGeneraciones");

        jSlider1.setMajorTickSpacing(10);
        jSlider1.setPaintLabels(true);
        jSlider1.setPaintTicks(true);
        jSlider1.setValue(40);

        jLabel4.setFont(new java.awt.Font("Comic Sans MS", 1, 14));
        jLabel4.setText("Probabilidad Cruce ( %)");

        jLabel5.setFont(new java.awt.Font("Comic Sans MS", 1, 14));
        jLabel5.setText("Probabilidad Mutación  ( %)");

        jButton1.setBackground(new java.awt.Color(51, 255, 102));
        jButton1.setFont(new java.awt.Font("Comic Sans MS", 1, 12));
        jButton1.setText("Calcular");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setFont(new java.awt.Font("Comic Sans MS", 3, 12));
        jButton2.setText("Ver Gráficas");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        buttonGroup2.add(Ruleta);
        Ruleta.setText("Ruleta");

        buttonGroup2.add(Torneo);
        Torneo.setSelected(true);
        Torneo.setText("Torneo");

        jLabel7.setFont(new java.awt.Font("Comic Sans MS", 1, 14));
        jLabel7.setText("Tipo Selección");

        jLabel8.setFont(new java.awt.Font("Comic Sans MS", 1, 14));
        jLabel8.setText("Tipo Cruce");

        buttonGroup2.add(Estocastico);
        Estocastico.setText("Estocastico");

        buttonGroup2.add(Restos);
        Restos.setText("Restos");

        buttonGroup2.add(Ranking);
        Ranking.setText("Ranking");

        tipoCruceJ.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "CrucePMX", "CruceOX", "CruceOXposicionesPrioritarias", "CruceOXposicionesOrden", "CrucePorCiclos", "CrucePorRecombinacionRutas", "CrucePorCodificacionOrdinal", "CrucePar-Impar" }));

        jLabel9.setFont(new java.awt.Font("Comic Sans MS", 1, 14));
        jLabel9.setText("Tipo Mutacion");

        tipoMutacionJ.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "mutacionInversion", "mutacionIntercambio", "mutacionInsercion", "mutacionHeuristica", "mutacionPropia" }));

        jLabel10.setFont(new java.awt.Font("Comic Sans MS", 1, 14));
        jLabel10.setText("Presion Selectiva");

        presionSelectiva.setText("1");

        jLabel11.setFont(new java.awt.Font("Comic Sans MS", 2, 10));
        jLabel11.setText(" Entre 1 y 2");

        buttonGroup2.add(SeleccionPropia);
        SeleccionPropia.setText("Propia");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(PanelGraficas, javax.swing.GroupLayout.DEFAULT_SIZE, 956, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(PanelDatos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(jTextField2))
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGroup(layout.createSequentialGroup()
                                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(Estocastico)
                                                        .addComponent(Torneo))
                                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addGroup(layout.createSequentialGroup()
                                                            .addComponent(Ranking)
                                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                            .addComponent(SeleccionPropia))
                                                        .addGroup(layout.createSequentialGroup()
                                                            .addComponent(Restos)
                                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                            .addComponent(Ruleta)))))))
                                    .addComponent(CbElitismo, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(46, 46, 46)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, 254, Short.MAX_VALUE)
                                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                                    .addComponent(jLabel8)
                                    .addComponent(jLabel9)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel10)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(jSlider1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jSlider2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 227, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(10, 10, 10)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(tipoMutacionJ, javax.swing.GroupLayout.Alignment.TRAILING, 0, 242, Short.MAX_VALUE)
                                            .addComponent(tipoCruceJ, 0, 242, Short.MAX_VALUE)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(presionSelectiva, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)))))))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(870, 870, 870))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(43, 43, 43)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(591, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel4)
                                    .addComponent(PanelDatos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jSlider2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel5)))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tipoCruceJ, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel7)
                                .addComponent(Estocastico)
                                .addComponent(Restos)
                                .addComponent(Ruleta)
                                .addComponent(jLabel8)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(44, 44, 44)
                                .addComponent(CbElitismo)
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jButton2)))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(33, 33, 33)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel9)
                                    .addComponent(tipoMutacionJ, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel10)
                                    .addComponent(presionSelectiva, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel11))))
                        .addGap(31, 31, 31)
                        .addComponent(PanelGraficas, javax.swing.GroupLayout.PREFERRED_SIZE, 202, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(155, 155, 155)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(Torneo)
                            .addComponent(Ranking, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(SeleccionPropia))))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        String poblacion = jTextField1.getText();
        String numGeneraciones = jTextField2.getText();
        Double presionS = Double.parseDouble(presionSelectiva.getText());
        boolean elitismo = CbElitismo.isSelected();
        double pc = jSlider1.getValue()/100.0;
        double pm = jSlider2.getValue()/100.0;
       
        
        //boolean torneo = jRadioButton2.isSelected();
        
        int tipoSeleccion=0;
        if (Torneo.isSelected())
        	tipoSeleccion=2;
        else if (Ruleta.isSelected())
        	tipoSeleccion=1;
        else if (Estocastico.isSelected())
        	tipoSeleccion=3;
        else if (Restos.isSelected())
        	tipoSeleccion=4;
        else if (Ranking.isSelected())
        	tipoSeleccion=5;
        else if (SeleccionPropia.isSelected())
                tipoSeleccion=6;
        int tipoCruce = tipoCruceJ.getSelectedIndex()+1;
        int tipoMutacion = tipoMutacionJ.getSelectedIndex()+1;
        //double precioKm = Double.parseDouble(kmText.getText());
        
        dt = Agenetico.ejecuta(elitismo, Integer.parseInt(poblacion), Integer.parseInt(numGeneraciones), pc, pm,tipoSeleccion,tipoMutacion,tipoCruce,presionS);
        String resultado = "El mejor individuo ha sido el de fenotipo \n" + dt.getElMejor().getFenotipo();
        resultado+= " \n \n";
        resultado+= " El individuo ha recorrido un total de : " + ((CromosomaCiudades)dt.getElMejor()).evaluar() + " km \n";
       // resultado+= " Con un coste econ�mico : " + ((CromosomaCiudades)dt.getElMejor()).dameCosteEconomico().getH1() + " Euros \n";
       // resultado+= " Invirtiendo un total de  : " + ((CromosomaCiudades)dt.getElMejor()).dameCosteEconomico().getH2() + " dias \n";
        
        PanelResultado.setText(resultado);
        

    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        if(dt!=null){
    	Graficas g = new Graficas();
         JFreeChart j =  g.pintarGrafica("funcion", dt.getElem1(), dt.getElem2(), dt.getElem3());
          ChartPanel p  =  new ChartPanel(j);
           JFrame ventana = new JFrame("El grafico");
            ventana.getContentPane().add(p);
            ventana.pack();
            ventana.setVisible(true);
            //ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
        else{
        	JOptionPane.showMessageDialog(this, "Debe ejecutar el algoritmo antes de visualizar la gr�fica");
        }
         
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new InterfazAG().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox CbElitismo;
    private javax.swing.JRadioButton Estocastico;
    private javax.swing.JPanel PanelDatos;
    private javax.swing.JScrollPane PanelGraficas;
    private javax.swing.JTextPane PanelResultado;
    private javax.swing.JRadioButton Ranking;
    private javax.swing.JRadioButton Restos;
    private javax.swing.JRadioButton Ruleta;
    private javax.swing.JRadioButton SeleccionPropia;
    private javax.swing.JRadioButton Torneo;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JSlider jSlider1;
    private javax.swing.JSlider jSlider2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField presionSelectiva;
    private javax.swing.JComboBox tipoCruceJ;
    private javax.swing.JComboBox tipoMutacionJ;
    // End of variables declaration//GEN-END:variables

}
